package com.sxc.xmlAdapter;

import com.sxc.bean.Field;
import com.sxc.bean.Head;
import com.sxc.bean.HeadPlus;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class HeadAdapter extends XmlAdapter<Head, HeadPlus> {
    @Override
    public HeadPlus unmarshal(Head head) throws Exception {
        HeadPlus headPlus = new HeadPlus();
        for(java.lang.reflect.Field reflectField : HeadPlus.class.getDeclaredFields()){

            String value = head.getFieldList().stream()
                    .filter(f -> f.getName().compareToIgnoreCase(reflectField.getName())==0)
                    .map(Field::getValue).findAny().get();

            List<Method> methods = List.of(HeadPlus.class.getDeclaredMethods());
            Method method = methods.stream()
                    .filter(f -> f.getName().compareToIgnoreCase("set" + reflectField.getName()) == 0)
                    .findAny().get();

            method.invoke(headPlus,value);
        }

        return headPlus;
    }

    @Override
    public Head marshal(HeadPlus head) throws Exception {

        return null;
    }
}
